/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.utility.connection;
/**
 *
 * @author esha
 */
import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.form.Gamecontrol;
import com.hitechbangla.fctu.utility.CustomSerializer;
import com.hitechbangla.fctu.utility.GeneralUtil;
import fctu.simulator.HTB_MAIN;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class  GetClientsConnection extends JFrame implements ActionListener {
    String uname;
    PrintWriter pw;
    BufferedReader br;
    JTextArea  taMessages;
    JTextField tfInput;
    //JButton btnSend,btnExit,btnStart;
    Socket client;
    HTB_MAIN parent;
    

    public GetClientsConnection(String uname,String servername, HTB_MAIN p) throws Exception {
        super("Connection Wizard");  // set title for frame
        if("".equals(uname))
        {
            uname = InetAddress.getLocalHost().getHostName();
        }
        this.uname = uname;
        this.parent = p;
        client  = new Socket(servername,8000);
        br = new BufferedReader( new InputStreamReader( client.getInputStream()) ) ;
        pw = new PrintWriter(client.getOutputStream(),true);
        pw.println(uname);  // send name to server
        parent.isNetworkConnected = true;
        parent.getClientConnection = this;
        buildInterface();
        new GetClientsConnection.MessagesThread().start();  // create thread for listening for messages
    }

    public void buildInterface() {
        //btnSend = new JButton("Send");
        //btnExit = new JButton("Exit");
        
        //btnStart = new JButton("Start Game");
        //btnOpen.setActionCommand("Button1");
        taMessages = new JTextArea();
        taMessages.setRows(10);
        taMessages.setColumns(50);
        taMessages.setEditable(false);
        tfInput  = new JTextField(50);
        JScrollPane sp = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(sp,"Center");
        JPanel bp = new JPanel( new FlowLayout());
       // Jframe jp=new JFrame("");
       // bp.add(tfInput);
        //bp.add(btnSend);
        //bp.add(btnExit);
        
        //bp.add(btnStart);
        //add(bp,"South");
        
        //btnSend.addActionListener(this);
        //btnExit.addActionListener(this);
        
        //btnStart.addActionListener(this);
        setSize(500,300);
        setVisible(true);
        pack();
        
        
        
    }

    public void actionPerformed(ActionEvent evt) {
       
    }

    public void SendGameScene() {
        
            
            //parent.frameGameControl.gamePauseNow();
             String gsString = CustomSerializer.objectToString(parent.gameScene);
             pw.println(gsString);
             
//             String gStart = "START";
//             pw.println(gStart);
             parent.frameGameControl.gameStartNow();
             parent.paintIt();
    }
    
     public void SendGameCommand(String command) {
            
             String comm = "COMM:" + command;
             pw.println(comm);
             
//             String gStart = "START";
//             pw.println(gStart);
             //parent.frameGameControl.gameStartNow();
    }
     
     
     public void SendGameStop() 
    {
        //JOptionPane.showMessageDialog(null, "send game stop");
        pw.println("STOP");
    }
    
    public void SendGameStart() 
    {
        //JOptionPane.showMessageDialog(null, "send game start");
        pw.println("START");
    }

    // inner class for Messages Thread
    class  MessagesThread extends Thread {
        public void run() {
            String line;
            try {
                while(true) {
                    line = br.readLine();
                    if(line.length() > 45) {
                        //parent.frameGameControl.gamePauseNow();
                      GameScene gs = CustomSerializer.stringToObject(line);
                      parent.gameScene = gs;
                      gs.HTBParent = parent;
                      gs.HTBParent.frameGameControl.gameStartNow();
                      
                    }
                    else if (line.equals("STOP")) {
                        //JOptionPane.showMessageDialog(null, "receive game stop");
                        parent.frameGameControl.gamePauseNow();
                        
                    }
                    else if(line.equals("START")) {
                        //JOptionPane.showMessageDialog(null, "receive game start");
                        parent.frameGameControl.gameStartNow();
                    }
                    else if (line.split(":")[0].equals("COMM")) {
                        String command = line.split(":")[1];
                        GeneralUtil.executeCommand(parent.gameScene, command);
                    }
                    else {
                        taMessages.append(line + "\n");
                    }
                } // end of while
            } catch(Exception ex) {}
        }
    }
} //  end of client


 
